package cn.wolfcode.trip.admin.controller;

import cn.wolfcode.trip.base.domain.Product;
import cn.wolfcode.trip.base.domain.ProductContent;
import cn.wolfcode.trip.base.query.ProductQueryObject;
import cn.wolfcode.trip.base.service.IProductCatalogService;
import cn.wolfcode.trip.base.service.IProductService;
import cn.wolfcode.trip.base.util.UploadUtil;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

/**
 * @author by mycp
 * @Classname ProductController
 * @Description TODO 积分商品处理器
 * @Date 2018/11/7 22:05
 */
@Controller
@RequestMapping("/product")
public class ProductController {
    @Autowired
    private IProductService productService;
    @Autowired
    private IProductCatalogService productCatalogService;

    /**
     * 高查分页积分商品
     * @param qo
     * @param model
     * @return
     */
    @RequestMapping("/list")
    public String list(@ModelAttribute("qo") ProductQueryObject qo, Model model) {
        PageInfo pageInfo = productService.query(qo);
        model.addAttribute("pageInfo", pageInfo);

        // 查询所有不被禁用的攻略
        model.addAttribute("catalogs", productCatalogService.selectEnable());
        return "product/list";
    }

    /**
     * 新增 / 修改
     * @param product
     * @param img
     * @return
     */
    @RequestMapping("/saveOrUpdate")
    public String saveOrUpdate(Product product, MultipartFile img) {
        //判断文件是否有值
        if (img != null && img.getContentType().startsWith("image") && img.getSize() > 0) {
            String url = UploadUtil.upload(img);
            product.setCoverUrl(url);
        }
        productService.saveOrUpdate(product);
        return "redirect:/product/list.do";
    }

    /**
     * 获取指定积分商品的内容
     */
    @RequestMapping("/getContent")
    @ResponseBody
    public Object getContentById(Long id){
        ProductContent content = productService.getContentById(id);
        return content;
    }
}
